/*
 * =====================================================================================
 *
 *       Filename:  movedir.c
 *
 *    Description:  Move Directory
 *
 *        Version:  1.0
 *        Created:  Monday 15 October 2012 11:28:09  IST
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include<stdio.h>
#include "../include/test.h"
#include "../include/Hash.h"
#include <string.h>
#include <stdlib.h>

//void renameTraverse(struct Node *node,int dest_file_descriptor_no,int src_file_descriptor_no);
void renameTraverse(struct Node *node);
void reHash(int fdnum);
	void moveNode(int src_file_descriptor_no, int dest_file_descriptor_no){
			struct Node *src , *dest,*temp,*prev,*srcSibling;
			//printf("\ninside move node1\n");
			//printf("src_file_descriptor_no = %d\n",src_file_descriptor_no);
			src = searchNode(src_file_descriptor_no);
			//printf("src->file_descriptor_no = %d\n",src->file_descriptor_no);
			srcSibling = src->sibling;
			dest = searchNode(dest_file_descriptor_no);
			if(dest->child == NULL){
				dest->child = src;
				dest->child->sibling = NULL; // now i added this line
			}
			else{
				temp = dest->child;
				if(dest->child->file_descriptor_no > src_file_descriptor_no)
					{
					dest->child = src;
					src->sibling = temp;
				}
				else{
					while(temp!=NULL&&(temp->file_descriptor_no < src->file_descriptor_no)){
						prev = temp;
						temp = temp->sibling;
					}
					prev->sibling = src;
					src->sibling = temp;
				}
			}
			temp = searchNode(header.fd[src_file_descriptor_no].parent_file_descriptor_no);
                               
			if(temp->child == src)
				temp->child = srcSibling;//before changing it was src->sibling, but this has to be change as src->sibling is changed in the previous if condition
			else{
				temp =temp->child;
				while(temp!=NULL&&(temp->file_descriptor_no < src->file_descriptor_no)){
					prev = temp;
					temp = temp->sibling;
				}
				prev->sibling = srcSibling;//before changing it was temp->sibling, but this has to be change as src->sibling is changed in the previous if condition
			}
                        //change the location path of the file descriptor
  			//rehash into hash table with the different path
                        header.fd[src_file_descriptor_no].parent_file_descriptor_no = dest_file_descriptor_no;
			
                        //reHash(src_file_descriptor_no)	;
			
			renameTraverse(src);
	               		
			return;
	}
	void renameTraverse(struct Node *node){
            /*		
             struct Node * temp;
             temp=node->child;
             while(temp!=NULL)
             {
                 struct Node * sibNode;
                 sibNode=temp;
                 while(sibNode!=NULL)
                 {
                   
                   reHash(sibNode->file_descriptor_no); 
                   sibNode=sibNode->sibling;

                 }
                temp=temp->child;  
             }
             */
             if(node==NULL)
             	return;
    //         printf("In renameTraverse File Descriptor : %d\n",node->file_descriptor_no);
             reHash(node->file_descriptor_no);
             renameTraverse(node->child);
             renameTraverse(node->sibling);

	}




   void reHash(int fdnum)
   {

   //delete from hash table
  
   delete_from_hash_table(header.fd[fdnum].path,fdnum); 
   //change the location path of the file descriptor
   int loc_path_len = strlen(header.fd[header.fd[fdnum].parent_file_descriptor_no].location_path);
  // printf("location_path :: %s\n",header.fd[header.fd[fdnum].parent_file_descriptor_no].location_path);
  // printf("path :: %s\n",header.fd[header.fd[fdnum].parent_file_descriptor_no].path);
  // printf("File Descriptor= %d\n",fdnum);
  // printf("Parent File Descriptor= %d\n",header.fd[fdnum].parent_file_descriptor_no);
  if(header.fd[fdnum].parent_file_descriptor_no !=0){

   sprintf(header.fd[fdnum].path,"%s/%s",header.fd[header.fd[fdnum].parent_file_descriptor_no].path,header.fd[fdnum].file_name);
    }else{
//		sprintf(header.fd[fdnum].location_path,"%s%d",header.fd[header.fd[fdnum].parent_file_descriptor_no].location_path,fdnum);
	   sprintf(header.fd[fdnum].path,"%s%s",header.fd[header.fd[fdnum].parent_file_descriptor_no].path,header.fd[fdnum].file_name);
    
    }
   sprintf(header.fd[fdnum].location_path,"%s/%d",header.fd[header.fd[fdnum].parent_file_descriptor_no].location_path,fdnum);    
   //rehash into hash table with the different path
   
   add_to_hash_table(header.fd[fdnum].path,fdnum); 
    
   }

